<?php
require_once 'lib/controllerWithoutGetOrPost.php';


function pageNavLink($totalpages, $pagenumber, $currentpage, $firstname, $surname) {
	if($pagenumber <= $totalpages) {
		if($currentpage != $pagenumber) {
	 		return "<a href=\"searchpeople.php?surname=".$surname."&firstname=".$firstname."&page=".$pagenumber."\">".$pagenumber."</a>\n\t";	
		} else {
			return "<strong>[ ".$pagenumber." ]</strong>\n\t";
		}
	}
}

function insertSearchPageTopNavigation($surname, $firstname, $totalpages, $currentpage) {

$startpage = $currentpage;
if($currentpage < 3) {
	$startpage = 3;
}

$page1 = $startpage-2;
$page2 = $startpage-1;
$page3 = $startpage;
$page4 = $startpage+1;
$page5 = $startpage+2;

echo "<li class=\"sortby\">";
echo "<a href=\"searchpeople.php?surname=".$surname."&firstname=".$firstname."&page=1\">&lt;&lt;</a>\n\t";
echo pageNavLink($totalpages, $page1, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page2, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page3, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page4, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page5, $currentpage, $firstname, $surname);
echo "...\n\t";
echo "<a href=\"searchpeople.php?surname=".$surname."&firstname=".$firstname."&page=".$totalpages."\">".$totalpages."</a>\n\t";
echo "<a href=\"searchpeople.php?surname=".$surname."&firstname=".$firstname."&page=".$totalpages."\">&gt;&gt;</a>\n\t";
echo "</li>";
						
	
}

function insertSearchPageBottomNavigation($surname, $firstname, $totalpages, $currentpage) {

$startpage = $currentpage;
if($currentpage < 3) {
	$startpage = 3;
}

$page1 = $startpage-2;
$page2 = $startpage-1;
$page3 = $startpage;
$page4 = $startpage+1;
$page5 = $startpage+2;

echo "<li class=\"sortby\">";
echo "<a href=\"#topbar\" title=\"Back to Top\" class=\"leftfloat\">Back to Top</a>\n\t";
echo "<a href=\"searchpeople.php?surname=".$surname."&firstname=".$firstname."&page=1\">&lt;&lt;</a>\n\t";
echo pageNavLink($totalpages, $page1, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page2, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page3, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page4, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page5, $currentpage, $firstname, $surname);
echo "...\n\t";
echo "<a href=\"searchpeople.php?surname=".$surname."&firstname=".$firstname."&page=".$totalpages."\">".$totalpages."</a>\n\t";
echo "<a href=\"searchpeople.php?surname=".$surname."&firstname=".$firstname."&page=".$totalpages."\">&gt;&gt;</a>\n\t";
echo "</li>";
					
						
	
}

$firstname = trim($_GET['firstname']);
$surname = trim($_GET['surname']);

if($firstname == "First Name") {
	$firstname = "";
}

$query = "select 	Person.ID, 
	Person.Surname, 
	Person.FirstNames, 
	Person.FormerName, 
	Person.NeeName, 
	Person.AlsoKnownAs, 
	Person.DeathDate, 
	Person.BirthDate, 
	Person.Age AS 'Age', 
	Person.AgeMeasure AS 'AgeMeasure', 
	CityTown.Name as 'CityTown', 
	c2.Name as 'FormerCityTown', 
	Region.Name AS 'Region', 
	Country.Name AS 'Country', 
	Charity.Name AS 'Charity', 
	FuneralHome.Name AS 'FuneralHome',
  Person.OtherInfo As 'Other Information'

from Person 
left join Region on Person.RegionID = Region.ID 
left join Country on Country.ID = Person.CountryID 
left join CityTown AS c2 on Person.FormerCityTownID = c2.ID 
left join Charity on Charity.ID = Person.CharityID 
left join FuneralHome on FuneralHome.ID = Person.FuneralHomeID 
left join CityTown on CityTown.ID = Person.CityTownID";

$blah = "WHERE (Person.Surname Like '".mysql_real_escape_string($surname)."%' OR Person.NeeName Like '".mysql_real_escape_string($surname)."%' OR Person.FormerName Like '".mysql_real_escape_string($surname)."%') AND (Person.FirstNames Like '".mysql_real_escape_string($firstname)."%' OR Person.AlsoKnownAs Like '".mysql_real_escape_string($firstname)."%') ORDER BY Person.DeathDate DESC LIMIT ";


$querycount = "select count(*) from Person WHERE (Person.Surname Like '".mysql_real_escape_string($surname)."%' OR Person.NeeName Like '".mysql_real_escape_string($surname)."%' OR Person.FormerName Like '".mysql_real_escape_string($surname)."%') AND (Person.FirstNames Like '".mysql_real_escape_string($firstname)."%' OR Person.AlsoKnownAs Like '".mysql_real_escape_string($firstname)."%')";


$resultcount = mysql_query($querycount, $link);
$resultcount = mysql_result($resultcount, 0);

$totalpages = 1;
$resultsperpage = 10;

$totalpages = ceil($resultcount/$resultsperpage);

$from = 0;
$to = $resultsperpage;
$currentpage = 1;

$pagenumber = intval($_GET['page']);
if($pagenumber > 0) {
    $currentpage = $pagenumber;
    $to = $pagenumber*$resultsperpage;
    $from = $to-$resultsperpage;
    $blah = $blah.$from.",".$resultsperpage;	
} else {
    $blah = $blah."0,".$resultsperpage;
}

if($pagenumber > 0) {
	if($pagenumber < $totalpages) {
		$pagenumber += 1;
	}	
} else {
	$pagenumber += 2;
}



$query .= ' '.$blah;

//echo $query;

// Perform Query
$result = mysql_query($query, $link);

$class1 = "odd";
$class2 = "even";
$class = $class1;

function showage($age) {
	if($age > 0) {
		return "Age ".$age;
	}
}

function formatdatetime($date) {
	if($date == "0000-00-00 00:00:00") {
		return "";
	}   else {
		return date('d F Y', strtotime(str_replace("&", "&amp;", $date)));
	}
}


function showbirthdate($birthdate) {
	if(strlen($birthdate) > 0) {
		return " <strong>b.</strong> ".$birthdate;
	}
}

function showdeathdate($deathdate) {
	if(strlen($deathdate) > 0) {
		return "Died ".$deathdate.". ";
	}
}


function showformerly($former) {
	if(strlen($former) > 0) {
		return " ".$former; 
	}

}

function showaka($aka) {
	if(strlen($aka) > 0) {
		return "Also known as <b> ".$aka."</b>."; 
	}
	
}

function shownee($nee) {
	if(strlen($nee) > 0) {
		return " ".$nee;
	}
}

function showageextra($age, $extra) {
	if(strlen(showage($age))> 0) {
		return $extra;
	}

}

function showneeandformer($nee, $former) {
  if(strlen(shownee($nee)) > 0 || strlen(showformerly($former)) > 0) {
    return "<p>Other Name(s):<b>".shownee($nee).showformerly($former)."</b>.</p>";
  } else {
    return "";
  }
}

function showfirstname($first) {
	if(strlen($first) > 0) {
		return $first." ";
	}
}

echo "<ul id=\"searchresults\">";


if($surname != "" && $surname != NULL) { 
echo "<li class=\"header\"><h2>Professional Searching Results for ".showfirstname($firstname).$surname."</h2></li>";
//echo "<li class=\"resultinfo\"><span>(".$resultcount.") listing results &nbsp; (".$resultsperpage.") results per page &nbsp; (".$totalpages.") pages </span>&nbsp</li>";
echo "<li class=\"resultinfo\"><span>(0) listing results &nbsp; (".$resultsperpage.") results per page &nbsp; (0) pages </span>&nbsp</li></ul>";
echo "<ul id=\"centerbar\">";
echo "<li>Dear ".$firstname."</li>";
echo "<li><p>Sorry but we do not have any professionals searching for the name you provided.</p>
<p><strong>Have you ever been known by another name?</strong></p>
<p>You may be remembered by a name you have used previously. Or the professional who is seeking you has the incorrect spelling or only your short (nick) name to search by.</p>
<p>Please try again using alternate spelling of your name. If you have no luck today please try again another day.</p>
<p><a title=\"Tell a Friend\" class=\"browsernav_tellfriend\" href=\"mailto:?subject=I personally recommend you visit this site&body=Hi, I'm letting you know that you can see if someone is searching for you by going to http://www.amemorytree.co.nz and entering your name in the Professionals Searching area.
Legal Professionals use this to make contact with people just like us. Your details aren't given to anyone, it’s FREE and you don't need to register and it 10 seconds. Good Luck!
\">Tell a Friend</a> about <a href=\"http://www.amemorytree.co.nz\">www.amemorytree.co.nz</a> today.</p></li>";

//this <a href=\"#\" title=\"\">Search Tips</a> goes between the above </span> and </li>...
//insertSearchPageTopNavigation(trim($_GET['surname']), trim($_GET['firstname']), $totalpages, $currentpage);

//echo "<li><ul>";


//if(mysql_num_rows($result)!=0) {
//while ($row = mysql_fetch_assoc($result)) {

//echo "<li>";



//echo .showbirthdate(formatdatetime($row['BirthDate'])).."<br /><br />";


//echo "<p class=\"results_name\"><a href=\"remembrance.php?personid=".$row['ID']."\" title=\"\" class=\"mainname\">".$row['Surname'].", ".$row['FirstNames']."</a>. ";
//echo showaka($row['AlsoKnownAs'])."</p>";
//echo showneeandformer($row['NeeName'], $row['FormerName']);
//echo "<p>".showdeathdate(formatdatetime($row['DeathDate'])).showage($row['Age']).showageextra($row['Age'],$row['AgeMeasure'])."</p>";
					
//echo "<p>Remembrance Page Open <a href=\"leaveamessage.php?personid=".$row['ID']."\">sign page</a> <a href=\"remembrance.php?personid=".$row['ID']."\">view entries</a></p>";
					
					

//	$query2 = "select count(*) from Publication left join PublicationPerson on Publication.ID = PublicationPerson.PublicationID where PublicationPerson.PersonID = ".$row['ID'];
//	$result2 = mysql_query($query2, $link);

	// Check result
	// This shows the actual query sent to MySQL, and the error. Useful for debugging.
//	if (!$result2) {
//	    $message  = 'Invalid query: ' . mysql_error() . "\n";
//	    $message .= 'Whole query: ' . $query;
//	    die($message);
//	}
//	$counter = 0;

//	if(mysql_num_rows($result2)!=0) {
	//echo "<strong>Public Notices on File</strong> "; 
  //    echo "<p class=\"left\">Published Notices (".mysql_result($result2, 0).") <a href=\"personnotices.php?personid=".$row['ID']."\" title=\"view notices\">view notices</a></p>";
  //} else {
   //   echo "<p class=\"left\">Published Notices (0) <a href=\"personnotices.php?personid=".$row['ID']."\" title=\"view notices\">view notices</a></p>";
  //}
	//echo "</div>";
  
  //echo "<p>Remembrance Page Open <a href=\"#\" title=\"sign page\">sign page</a> <a href=\"#\" title=\"view entries\">view entries</a></p>";
  //echo "<p class=\"right\">".$row['Country']."&nbsp</p>";
  //echo "</li>";
//}


//insertSearchPageBottomNavigation(($_GET['surname']), trim($_GET['firstname']), $totalpages, $currentpage);



} else {


echo "<li><p>Sorry we have no matches for the names you supplied.</p></li>";


}

echo "</ul>";
// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
mysql_close($link);
?>





